package com.cakin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.cakin.constant.Constant.USER_SESSION;

/**
 * @ClassName: logout
 * @Description: 注销
 * @Date: 2020/6/16
 * @Author: cakin
 */
public class Logout extends HttpServlet {
    /**
     * 功能描述：用户注销逻辑
     *
     * @author cakin
     * @date 2020/6/16
     * @param req 请求
     * @param resp 响应
     * @throws IOException IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object attribute = req.getSession().getAttribute(USER_SESSION);
        if (attribute != null) {
            // session失效
            req.getSession().removeAttribute(USER_SESSION);
        }
        // 重定向到登录页
        resp.sendRedirect("/login.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
